package com.chii.color.controller;

import com.alibaba.fastjson.JSONObject;
import com.chii.color.client.ImageClient;
import com.chii.color.config.MQConfig;
import com.chii.color.po.ResponeInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.UUID;

@Api(value = "测试后端相关controller", tags = {"测试相关"})
@RestController
@RequestMapping(value = "/test")
public class TestController {
    private Logger logger = LogManager.getLogger(TestController.class);
    @Autowired
    private ImageClient imageClient;
    @ApiOperation(value = "用于日志文件测试")
    @RequestMapping(value = "/checkLog", method = RequestMethod.GET)
    public ResponeInfo check() {
        System.out.println("测试开始");

        long start = System.currentTimeMillis();
        long startError = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            logger.error("error： " + i);
        }
        System.out.println("error使用时间:" + (System.currentTimeMillis() - startError));
        long startInfo = System.currentTimeMillis();
        for (int i = 0; i < 500000; i++) {
            logger.info("info： " + i);
        }
        System.out.println("info使用时间:" + (System.currentTimeMillis() - startInfo));
        String time = "使用时间:" + (System.currentTimeMillis() - start);
        ResponeInfo responeInfo = new ResponeInfo();
        responeInfo.setData(time);
        return responeInfo;
    }

    @ApiOperation(value = "welcome")
    @RequestMapping(value = "/welcome", method = RequestMethod.GET)
    public ResponeInfo welcome() {
        System.out.println("测试开始");
        ResponeInfo responeInfo = new ResponeInfo();
        responeInfo.setData("测试开始");
        return responeInfo;
    }

    /**
     * 测试返回异常信息
     *
     * @return String
     */
    @RequestMapping(value = "/exception", method = RequestMethod.GET)
    public String returnExceptionInfo(HttpServletRequest request,
                                      HttpServletResponse response) throws Exception {
        if (1 != 2) {
            // 用户民错误或不存在异常
            int i = 0;
            i = i / 0;
        }

        return "success";
    }

    @RequestMapping(value = "/ftp", method = RequestMethod.GET)
    public ResponeInfo ftp() throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();

        responeInfo.setData("测试开始");
        return responeInfo;
    }
    @RequestMapping(value = "/feign", method = RequestMethod.POST)
    public ResponeInfo feign(@ApiParam(name = "file", value = "上传图片列表") @RequestParam("file") MultipartFile file) throws Exception {
        String fileName = UUID.randomUUID().toString();
        ResponeInfo<String> info = imageClient.updateImage(file, fileName, "qiniu");
        return info;
    }
    @RequestMapping(value = "/feign64", method = RequestMethod.POST)
    public ResponeInfo feign64(@ApiParam(name = "base64", value = "上传图片列表") @RequestParam("base64") String base64) throws Exception {
        String fileName = UUID.randomUUID().toString();
        ResponeInfo<String> info = imageClient.updateBase64Image(base64, fileName, "qiniu");
        return info;
    }
    @Autowired
    private RabbitTemplate rabbitTemplate;
    @RequestMapping(value = "/rabbit", method = RequestMethod.GET)
    public ResponeInfo rabbit() throws Exception {
        ResponeInfo responeInfo = new ResponeInfo();
        JSONObject jsonObject = new JSONObject();
        File file = new File("123");
        jsonObject.put("uid", 1);

        jsonObject.put("render_factor", 111);
        jsonObject.put("file", file);
        rabbitTemplate.convertAndSend(MQConfig.QUEUE, jsonObject);
        responeInfo.setData("测试开始");
        return responeInfo;
    }
}
